package org.jeecg.modules.system.service.impl;

import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.jeecg.modules.system.service.FtpService;
import org.springframework.stereotype.Service;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

@Service
public class FtpServiceImpl implements FtpService {

    private final String server = "192.168.1.243"; // 替换为你的 FTP 服务器地址
    private final int port = 21; // 默认 FTP 端口
    private final String user = "mes"; // 替换为你的 FTP 用户名
    private final String pass = "xinhua@123"; // 替换为你的 FTP 密码

    public byte[] downloadFile(String filePath) throws IOException {
//        FTPClient ftpClient = new FTPClient();
//
//        try {
//            ftpClient.connect("192.168.1.243", 21);
//            ftpClient.enterLocalPassiveMode();
//            ftpClient.setControlEncoding("UTF-8");
//            boolean login = ftpClient.login("chint", "xinhua@123");
//            if (login) {
//                System.out.println("登录成功");
//            } else {
//                System.out.println("登录失败");
//                System.out.println("服务器响应: " + Arrays.toString(ftpClient.getReplyStrings()));
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                ftpClient.disconnect();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        try {
//            boolean exists = ftpClient.changeWorkingDirectory(filePath);
//            if (exists) {
//                System.out.println("文件路径存在");
//            } else {
//                System.out.println("文件路径不存在");
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        int replyCode = ftpClient.getReplyCode();
//        if (!FTPReply.isPositiveCompletion(replyCode)) {
//            System.out.println("权限不足");
//        } else {
//            System.out.println("权限正常");
//        }
//
//        return null;
        FTPClient ftpClient = new FTPClient();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try {
            // 连接到 FTP 服务器
            ftpClient.connect(server, port);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode(); // 使用被动模式
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 设置文件类型为二进制

            // 下载文件
            boolean success = ftpClient.retrieveFile(filePath, outputStream);
            if (!success) {
                throw new IOException("Failed to download file from FTP server");
            }
        }
        finally {
            // 关闭输出流和 FTP 连接
            outputStream.close();
            ftpClient.logout();
            ftpClient.disconnect();
        }

        return outputStream.toByteArray();
    }

    public void uploadFile(String filePath, MultipartFile file) throws IOException {
        FTPClient ftpClient = new FTPClient();
        InputStream inputStream = file.getInputStream();

        try {
            // 连接到 FTP 服务器
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode(); // 使用被动模式
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 设置文件类型为二进制

            // 上传文件
            boolean success = ftpClient.storeFile(filePath, inputStream);
            if (!success) {
                throw new IOException("Failed to upload file to FTP server");
            }
        } finally {
            // 关闭输入流和 FTP 连接
            inputStream.close();
            ftpClient.logout();
            ftpClient.disconnect();
        }
    }
}
